package com.mybase.app.UI.dialog;

import android.app.*;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.mybase.app.R;

import java.util.Calendar;

/**
 * Created by happy_000 on 2014/10/21.
 */

/***********
 *日期时间 数字 选择器,
 * 进度对话框
 *
 */
public class DialogActivity extends Activity implements View.OnClickListener{
    private DatePicker datePicker;
    private TextView textView;
    private NumberPicker numberPicker;
    private Button button1,button2,button3,button4,button5;
    private int year,day,month,minute,hour;
    private int value=15;
    private Button button21,button22,button23;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.widget_dialog_layout);
        Calendar calendar=Calendar.getInstance();
        year=calendar.get(Calendar.YEAR);
        month=calendar.get(Calendar.MONTH);
        day=calendar.get(Calendar.DAY_OF_MONTH);
        hour=calendar.get(Calendar.HOUR);
        minute=calendar.get(Calendar.MINUTE);
        textView= (TextView) findViewById(R.id.dialog_tv);
        datePicker= (DatePicker) findViewById(R.id.dialog_dpk);
        numberPicker= (NumberPicker) findViewById(R.id.dialog_npk);
        button1= (Button) findViewById(R.id.dialog_btn1);
        button2= (Button) findViewById(R.id.dialog_btn2);
        button3= (Button) findViewById(R.id.dialog_btn3);
        button21= (Button) findViewById(R.id.dialog_btn21);
        button22= (Button) findViewById(R.id.dialog_btn22);
        button23= (Button) findViewById(R.id.dialog_btn23);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button21.setOnClickListener(this);
        button22.setOnClickListener(this);
        button23.setOnClickListener(this);

        initPicker();

    }
    private void initPicker(){
        datePicker.init(year,month,day,new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year1, int monthOfYear, int dayOfMonth) {
                year=year1;
                month=monthOfYear;
                day=dayOfMonth;
                showDate(year,month,day,hour,minute,value);
            }
        });
        numberPicker.setMinValue(10);
        numberPicker.setMaxValue(50);
        //设置numberPicker的当前值
        numberPicker.setValue(value);
        numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener(){

            @Override
            public void onValueChange(NumberPicker picker, int oldVal,
                                      int newVal) {
                // TODO Auto-generated method stub
                value=newVal;
                showDate(year, month, day, hour, minute, value);
            }

        });
    }

    private void showDate(int yeat,int month,int day,int hour,int minute,int value) {
            textView.setText(""+yeat+"-"+month+"-"+day+" "+hour+"："+minute+"*"+value);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialog_btn1:
                AlertDialog.Builder builder=new AlertDialog.Builder(DialogActivity.this);
                builder.setTitle("对话框")
                        .setNeutralButton("确定",null)
                        .create().show();
                break;
            case R.id.dialog_btn2:
                ProgressDialog progressDialog=new ProgressDialog(DialogActivity.this);
                progressDialog.setTitle("进度对话框");
                progressDialog.setMax(100);
                progressDialog.setProgress(68);
                progressDialog.show();
                break;
            case R.id.dialog_btn3:
                final PopupWindow popupWindow=new PopupWindow(DialogActivity.this);
                popupWindow.showAsDropDown(button3);// 以下拉方式显示
                // popupWindow.showAtLocation(btn2, Gravity.CENTER,20,20);//在指定位置显示
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                            popupWindow.dismiss();//关闭
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            case R.id.dialog_btn21:
                Calendar c=Calendar.getInstance();
                new DatePickerDialog(
                        this,
                        new DatePickerDialog.OnDateSetListener() {
                            public void onDateSet(DatePicker dp, int year,int month, int dayOfMonth) {
                               // et.setText("您选择了：" + year + "年" + (month+1) + "月" + dayOfMonth + "日");
                            }
                        },
                        c.get(Calendar.YEAR), // 传入年份
                        c.get(Calendar.MONTH), // 传入月份
                        c.get(Calendar.DAY_OF_MONTH) // 传入天数
                ).show();
                break;
            case R.id.dialog_btn22:
                c=Calendar.getInstance();
                new TimePickerDialog(
                        this,
                        new TimePickerDialog.OnTimeSetListener(){
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                               // et.setText("您选择了："+hourOfDay+"时"+minute+"分");
                            }
                        },
                        c.get(Calendar.HOUR_OF_DAY),
                        c.get(Calendar.MINUTE),
                        false
                ).show();
                break;
            case R.id.dialog_btn23:

                break;
        }
    }
}
